package com.woniuxy.classteacher.service.impl;

import com.woniuxy.classteacher.mapper.*;
import com.woniuxy.classteacher.service.BadStudentPoolService;
import com.woniuxy.commons.entity.StudentInfo;
import com.woniuxy.commons.entity.TestPaper;
import com.woniuxy.commons.entity.wyh.GradeInfo;
import com.woniuxy.commons.entity.wyh.TeacherInfo;
import com.woniuxy.commons.entity.wyh.TeamInfo;
import com.woniuxy.commons.exception.NoBadStusException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 李英勇
 * @date 2020/7/24 - 15:13
 */
@Service
public class BadStudentServiceImpl implements BadStudentPoolService {
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Autowired
    private ClassTeacherMapper classTeacherMapper;
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private GradeMapper gradeMapper;
    @Autowired
    private ChangeClassMapper changeClassMapper;
    @Autowired
    private ClassMapper classMapper;

    /**
     * @Author: 李英勇
     * @Description: 在redis中查询差生池
     * @DateTime: 2020/8/5 16:25
     * @Params: [refreshToken]
     * @Return java.util.Map<java.lang.String, java.util.List < com.woniuxy.commons.entity.StudentInfo>>
     */
    @Override
    public Map<String, List<StudentInfo>> findBadStudent(String refreshToken) {
        Map<String, List<StudentInfo>> badStus = new HashMap<>();
        //在redis中获取用户的role和rid
        //通过refreshToken获取数据
        Map<Object, Object> data = redisTemplate.opsForHash().entries(refreshToken);
        //获取role
        String role = (String) data.get("role");
        //获取rid
        int rid = (int) data.get("rid");
        if ("teacher".equals(role)) {
            //通过rid获取老师的信息
            TeacherInfo teacherInfo = classTeacherMapper.findTeacherInfoByTcid(rid);
            //获取老师的学校
            String sname = teacherInfo.getSchoolInfo().getSname();
            //获取老师所教的班级
            //通过rid查询班主任id
            List<Integer> tmids = changeClassMapper.findTmIdByTeacherId(rid);
            if (tmids.size() != 0) {
                //通过tmids查询所管的所有班级
                List<TeamInfo> teamInfos = classMapper.findTeamInfoByTmId(tmids);
                //遍历班级获取每一个班级的差生
                for (int i = 0; i < teamInfos.size(); i++) {
                    TeamInfo teamInfo = teamInfos.get(i);
                    //获取班级、年级的名字
                    String tname = teamInfo.getTname();

                    //通过班级获取年级
                    GradeInfo gradeInfo = gradeMapper.findGradeByGaid(teamInfo.getGaId());
                    String gradeType = gradeInfo.getGradeType();

                    //在redis中查询对应班级的差生
                    Map<Object, Object> grade = redisTemplate.opsForHash().entries(sname);
                    if (grade.isEmpty()) {
                        throw new NoBadStusException("没有差生");
                    }
                    Map<String, Object> clazz = (Map<String, Object>) grade.get(gradeType);
                    List<StudentInfo> stus = (List<StudentInfo>) clazz.get(tname);

                    //放入map
                    badStus.put(tname, stus);
                }
            }

        }
        return badStus;
    }

    /**
     * @Author: 李英勇
     * @Description: 查询学生所有考试的试卷
     * @DateTime: 2020/8/5 16:25
     * @Params: [sid]
     * @Return java.util.List<com.woniuxy.commons.entity.TestPaper>
     */
    @Override
    public List<TestPaper> findTestPaperBySid(int sid) {
        //获取学生的所有试卷
        StudentInfo stu = studentMapper.findStuByStuid(sid);
        List<TestPaper> testPapers = stu.getTestPapers();
        return testPapers;
    }
}
